<template>
  <el-container>
    <el-header>
      <div class="homeTitle">
        <img src="../assets/heima.png" alt />
        <span>商品后台管理</span>
      </div>
      <el-button type="warning" @click="homeBack">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse? '64px':'200px'">
        <div class="toggle-button" @click="toggleCollapse">
          <i class="el-icon-open"></i>
        </div>
        <!-- <div class="toggle-button" @click="toggleCollapse">
          <i size="medium" class="el-icon-arrow-right"></i>
        </div>-->
        <el-menu
          :collapse="isCollapse"
          :collapse-transition="false"
          unique-opened
          router
          menu-trigger="click"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF"
          :default-active="activePath"
        >
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
            <template slot="title">
              <i :class="listIcon[item.id]" id="ico"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单区域 -->
            <el-menu-item
              :index="'/'+subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNavState('/' + subItem.path)"
            >
              <i class="el-icon-menu"></i>
              <span>{{ subItem.authName }}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧主题区域 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      isCollapse: false,
      menuList: null,
      listIcon: {
        '125': 'iconfont icon-user',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      activePath: ''
    }
  },
  created() {
    this.getList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    saveNavState(activePath) {
      // console.log(activePath)
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    },
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    homeBack() {
      window.sessionStorage.removeItem('token')
      this.$router.push('/login')
    },
    async getList() {
      var { data: res } = await this.$axios.get('menus')
      // console.log(res)
      if (res.meta.status !== 200) {
        return this.message.error('失败', res.meta.msg)
      }
      this.menuList = res.data
      // console.log(this.menuList)
    }
  }
}
</script>

<style lang="less" scoped>
.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
.el-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 20px;
  color: #fff;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
#ico {
  margin-right: 10px;
}
.toggle-button {
  height: 30px;
  text-align: center;
  > i {
    font-size: 20px;
    color: #fff;
    line-height: 20px;
  }
}
</style>
